/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package stack;

import accessori.Node;
import exceptions.EmptyStackException;

/**
 *
 * @author pc
 */
public class NodeStack<E> implements Stack<E> {

    private Node<E> top;
    private int size;

    public NodeStack() {
        top = null;
        size = 0;
    }

    @Override
    public int size() {
        return size;
    }

    @Override
    public boolean isEmpty() {
        return top == null ? true : false;
    }

    @Override
    public E top() throws EmptyStackException {
        if (top == null) {
            throw new EmptyStackException("Impossibile prelevare il top, stack vuoto");
        }
        return top.getElement();
    }

    @Override
    public void push(E o) {
        top = new Node<E>(o, top);
        size++;
    }

    @Override
    public E pop() throws EmptyStackException {
        if (isEmpty()) throw new EmptyStackException("Stack is empty.");
        E toReturn = top.getElement();
        top = top.getNext();
        size--;
        return toReturn;
    }
}
